84-largest-rectangle-in-histogram.py
problem: ---
problem:

Given an array of integers heights representing the histogram's bar height where the width of each 
bar is 1, return the area of the largest rectangle in the histogram.

Example 1:
Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.

Example 2:
Input: heights = [2,4]
Output: 4
 
Constraints:
1 <= heights.length <= 105
0 <= heights[i] <= 104
---

-----------------------------------------------------------------------
bug_fixes: ---
bug_fixes:
Replace `else 0` with `else -1` on line 10.
Replace `range(len(heights)-1, -1)` with `range(len(heights)-1, -1, -1)` on line 16.
---

-----------------------------------------------------------------------
bug_desc: ---
bug_desc:
On line 10, the value of 0 is appended to the lb list when the stack is empty. However, this value 0 is incorrect because it represents an valid index. Instead, it should be -1 because stack is None or missing.
On line 16, the step in the range is set to the default of 1. This will result in an infinite loop as the counter should decrease to -1. This can be fixed by setting the step to -1 like so: `range(len(heights)-1,-1,-1)`.
---

-----------------------------------------------------------------------
line_no: ---
line_no:
10
---

-----------------------------------------------------------------------
buggy_code: ---
buggy_code:
1. class Solution:
2.     def largestRectangleArea(self, heights: List[int]) -> int:
3.         lb = []
4.         stack = []
5.         
6.         for index in range(len(heights)):
7.             while stack and heights[stack[-1]] >= heights[index]:
8.                 stack.pop()
9.             
10.             lb.append(stack[-1] if stack else 0)
11.             stack.append(index)
12.         
13.         rb = [0] * len(heights)
14.         stack = []
15.         
16.         for index in range(len(heights)-1, -1):
17.             while stack and heights[stack[-1]] >= heights[index]:
18.                 stack.pop()
19.             
20.             rb[index] = stack[-1] if stack else len(heights)
21.             stack.append(index)
22.         
23.         max_area = 0
24.         for index in range(len(heights)):
25.             width = rb[index] - lb[index] - 1
26.             max_area = max(max_area, width * heights[index])
27.         
28.         return max_area
29. 
---

-----------------------------------------------------------------------
correct_code: ---
correct_code:
1. class Solution:
2.     def largestRectangleArea(self, heights: List[int]) -> int:
3.         lb = []
4.         stack = []
5.         
6.         for index in range(len(heights)):
7.             while stack and heights[stack[-1]] >= heights[index]:
8.                 stack.pop()
9.             
10.             lb.append(stack[-1] if stack else -1)
11.             stack.append(index)
12.         
13.         rb = [0] * len(heights)
14.         stack = []
15.         
16.         for index in range(len(heights)-1, -1, -1):
17.             while stack and heights[stack[-1]] >= heights[index]:
18.                 stack.pop()
19.             
20.             rb[index] = stack[-1] if stack else len(heights)
21.             stack.append(index)
22.         
23.         max_area = 0
24.         for index in range(len(heights)):
25.             width = rb[index] - lb[index] - 1
26.             max_area = max(max_area, width * heights[index])
27.         
28.         return max_area
29. 
---

-----------------------------------------------------------------------
